<?php
/**
 * File containing the IncrementProcessor
 *
 * PHP Version 5
 *
 * @category Processors
 * @package  Processors
 * @author   Martin Schlecht <herrlollinger@gmail.com>
 * @license  MIT License
 * @link     http://code.google.com/p/universal-test-data
 */

/**
 * Processor class "IncrementProcessor"
 *
 * Processor which is used to process tags which implements number incrementation within the text.
 *
 * @category   Processors
 * @package    Processors
 * @subpackage IncrementProcessors
 * @author     Martin Schlecht <herrlollinger@gmail.com>
 * @copyright  see @author
 * @license    MIT License
 * @link       http://code.google.com/p/universal-test-data
 *
 * The followings are the available attributes of the processor class
 * @property string $text, the text which should be processed
 */
class IncrementProcessor
{
	private $text;
	
	/**
	 * The processor randomizes the given parameters
	 *
	 * @param String $text, the text to process
	 *
	 * @return String
	 */
	public function process($text) {
		$this->text = $text;
		
		preg_match_all("#\s*{(.*?)}#si", $this->text, $matches);
		
		foreach ($matches[1] AS $m) {
			$string = "{" . $m . "}";
			$m = explode(":", $m);
			
			/**
			 * {incrementNumber:start,step,resetafter,resetstep}
			 */
			if ($m[0] == "incrementNumber") {
				$replacement = "";
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || !is_numeric($parameter[0])) {
					$parameter[0] = 1;
				}
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = 1;
				}
				
				if (!isset($parameter[2]) || !is_numeric($parameter[2])) {
					$parameter[2] = 1000000;
				}
				
				if (!isset(Yii::app()->session[$string . "_increment"]) || Yii::app()->session[$string . "_reset"] >= $parameter[2]) {
					
					Yii::app()->session[$string . "_increment"] = $parameter[0];
					Yii::app()->session[$string . "_reset"] = 1;
									
					$replacement = $parameter[0];
				} else {
					$increment = Yii::app()->session[$string . "_increment"];
					$replacement = $increment + $parameter[1];
					
					Yii::app()->session[$string . "_increment"] = $replacement;
					Yii::app()->session[$string . "_reset"] = Yii::app()->session[$string . "_reset"] + 1;
				}
				
				Yii::app()->session['resetManager']->add($string . "_increment");
				Yii::app()->session['resetManager']->add($string . "_reset");
					
				$this->text = str_replace($string, $replacement, $this->text);
			}
			
			/**
			 * {randomIncrementNumber:step,min,max}
			 */
			if ($m[0] == "randomIncrementNumber") {
				$replacement = "";
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || !is_numeric($parameter[0])) {
					$parameter[0] = 1;
				}
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = 10000;
				}
				
				if (!isset($parameter[2]) || !is_numeric($parameter[2])) {
					$parameter[2] = 100000;
				}
								
				if (!isset(Yii::app()->session[$string . "_increment"])) {
					$randomNumberProcessor = new RandomNumberProcessor();
					$replacement = $randomNumberProcessor->randomNumber($parameter[1], $parameter[2]);
					
					Yii::app()->session[$string . "_increment"] = $replacement;
				} else {
					$increment = Yii::app()->session[$string . "_increment"];
					$replacement = $increment + $parameter[0];
					
					Yii::app()->session[$string . "_increment"] = $replacement;
				}
				
				Yii::app()->session['resetManager']->add($string . "_increment");
					
				$this->text = str_replace($string, $replacement, $this->text);
			}
		}
		
		return $this->text;
	}
	
	/**
	 * Returns the documentation
	 *
	 * @return String
	 */
	 public function documentation() {
		return array(
			array('{incrementNumber:start,step,resetafter}', 'Incements the given number'), 
		);
	 }
	 
	/**
	 * Prints the documentation
	 *
	 * @return String
	 */
	public function print_documentation() {
		foreach ($this->documentation() AS $docu) {
			echo '<span style="font-family:monospace;font-size:11px">' . $docu[0] . '</span><br/>' . $docu[1] . '<br/>';
		}
	}
}
?>